package com.neusoft.neumooc.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.VO.GoodsVO;


/** 
 * @ClassName: GoodsDAOImpl 
 * @Description: TODO 商品管理业务数据访问类
 * @author zls
 * @date 2016年10月27日
 */
@Repository
public class GoodsDAOImpl extends BaseDAO implements GoodsDAO{

	/** 
	 * @Fields GET_SHOPLIST_SQL : TODO 获得商品列表
	 */ 
	private final static String GET_SHOPLIST_SQL = "SELECT wemall_good.id,wemall_good.menu_id as menuId,wemall_menu.name AS menuName,wemall_good.sort,wemall_good.name,wemall_good.price,wemall_good.old_price as oldPrice,wemall_good.image,wemall_good.detail,wemall_good.status,wemall_good.time,wemall_good.number FROM wemall_good LEFT JOIN wemall_menu ON wemall_good.menu_id = wemall_menu.id ORDER BY sort";
		
	/** 
	 * @Fields GET_SHOPLIST_NUMBER_SQL : TODO 获得商品列表计数
	 */ 
	private final static String GET_SHOPLIST_NUMBER_SQL ="SELECT COUNT(1) FROM wemall_good";
	
	/** 
	 * @Fields ADD_SHOP_SQL : TODO 增加商品
	 */ 
	private final static String ADD_SHOP_SQL = "INSERT INTO wemall_good(id,menu_id,sort,name,price,old_price,savepath,image,detail,status,time,number) VALUES(:id,:menuId,:sort,:name,:price,:oldPrice,:savePath,:image,:detail,:status,NOW(),:number)";
	
	/** 
	 * @Fields GET_SHOP_ITEM_SQL : TODO 获得更改商品信息
	 */ 
	private final static String GET_SHOP_INFO_SQL = "SELECT wemall_good.id,wemall_good.savepath AS savePath,wemall_good.menu_id AS menuId,wemall_menu.name AS menuName,wemall_good.sort,wemall_good.name,wemall_good.price,wemall_good.old_price as oldPrice,wemall_good.image,wemall_good.detail,wemall_good.status,wemall_good.time,wemall_good.number FROM wemall_good LEFT JOIN wemall_menu ON wemall_good.menu_id = wemall_menu.id WHERE wemall_good.id = ?";
	
	/** 
	 * @Fields EDIT_SHOP_SQL : TODO 更改商品
	 */ 
	private final static String EDIT_SHOP_SQL = "UPDATE wemall_good SET menu_id = :menuId, sort = :sort, name = :name, price = :price, old_price = :oldPrice, savepath = :savePath, image = :image, detail = :detail, status = :status, time = now(),number = :number WHERE id = :id";
	
	/** 
	 * @Fields DEL_SHOP_SQL : TODO 删除商品
	 */ 
	private final static String DEL_SHOP_SQL = "DELETE FROM wemall_good WHERE id = ?";
	
	/** 
	 * @Fields GET_GOODS_MENU_SQL : TODO 获得分类下的商品
	 */ 
	private final static String GET_GOODS_MENU_SQL = "SELECT id,sort,name,price,old_price as oldPrice,savepath AS savePath,image,detail,status,time,number FROM wemall_good WHERE menu_id = ?";
	
	/** 
	 * @Fields GET_GOODS_MENU_COUNT_SQL : TODO 获得分类下的商品计数
	 */ 
	private final static String GET_GOODS_MENU_COUNT_SQL = "SELECT COUNT(1) FROM wemall_good WHERE menu_id = ?";
	
	
	/** 
	 * @Title: getGoodsList 
	 * @Description: TODO 获得商品列表
	 * @param pageIndex 起始页数
	 * @param pageSize 页面大小
	 * @return List
	 * @throws DAOException
	 */
	@Override
	public List<GoodsVO> getGoodsList(int pageIndex, int pageSize) throws DAOException{
		return this.getPageData(GET_SHOPLIST_SQL, GoodsVO.class, pageIndex, pageSize);
	}

	
	/** 
	 * @Title: getGoodsCount 
	 * @Description: TODO 获得商品列表计数
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int getGoodsCount() throws DAOException{
		return this.queryForInteger(GET_SHOPLIST_NUMBER_SQL);
	}

	
	/** 
	 * @Title: addGoods 
	 * @Description: TODO 增加商品
	 * @param goodsVO 商品实体类
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int addGoods(GoodsVO goodsVO) throws DAOException {
		return this.update(ADD_SHOP_SQL,goodsVO);	
	}
	
	
	/** 
	 * @Title: getGoodsInfo 
	 * @Description: TODO 获得商品详细信息
	 * @param id 商品ID
	 * @return Map
	 * @throws DAOException
	 */
	@Override
	public Map<String, Object> getGoodsInfo(String id) throws DAOException {
		Object[] args = { id };
		return this.queryForMap(GET_SHOP_INFO_SQL,args);
	}
	
	
	/** 
	 * @Title: updateGoods 
	 * @Description: TODO 更改商品
	 * @param goodsVO 商品实体类
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int updateGoods(GoodsVO goodsVO) throws DAOException {
		return this.update(EDIT_SHOP_SQL,goodsVO);
	}
	
	
	/** 
	 * @Title: deleteGoods 
	 * @Description: TODO 删除商品
	 * @param id 商品ID
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int deleteGoods(String id) throws DAOException {
		Object[] args = { id };
		return this.update(DEL_SHOP_SQL,args);
	}


	/** 
	 * @Title: getGoodsMsg 
	 * @Description: TODO 获得商品详细内容
	 * @param id 商品ID
	 * @return List
	 * @throws DAOException
	 */
	@Override
	public List<GoodsVO> getGoodsMsg(String id) throws DAOException {
		Object[] args = { id };
		return this.queryForBeanList(GET_SHOP_INFO_SQL, GoodsVO.class,args);
	}

	
	/** 
	 * @Title: getListByMenu 
	 * @Description: TODO 获得分类下的商品
	 * @param pageIndex 起始页数
	 * @param pageSize 页面大小
	 * @param menuId 分类ID
	 * @return List
	 * @throws DAOException
	 */
	@Override
	public List<GoodsVO> getListByMenu(int pageIndex, int pageSize, String menuId) throws DAOException {
		Object[] args = { menuId };
		return this.getPageData(GET_GOODS_MENU_SQL, GoodsVO.class, pageIndex, pageSize, args);
	}

	
	/** 
	 * @Title: getMenuGoodsCount 
	 * @Description: TODO 获得分类下的商品计数
	 * @param menuId 分类ID
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int getMenuGoodsCount(String menuId) throws DAOException {
		Object[] args = { menuId };
		return this.queryInteger(GET_GOODS_MENU_COUNT_SQL, args);
	}
}
